package com.atguigu.gmall.web.all.controller;

import com.atguigu.gmall.feign.seckill.SeckillFeignClient;
import com.atguigu.gmall.feign.user.UserFeignClient;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.user.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author lsj
 * @create 2021-09-02 11:33
 */
@Controller
public class SeckillController {


    @Autowired
    SeckillFeignClient seckillFeignClient;

    @Autowired
    UserFeignClient userFeignClient;

    /**
     * 秒杀商品列表页
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String toSeckillPage(Model model){

        //查出所有参与当前时间点秒杀的商品

        List<SeckillGoods> currentSeckillGoods = seckillFeignClient.getCurrentSeckillGoods();
        //skuId\skuDefaultImg\skuName\costPrice\price\num\stockCount
        model.addAttribute("list",currentSeckillGoods);
        return "seckill/index";
    }


    /**
     * 秒杀商品详情页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String toSeckillGoodDetailPage(@PathVariable Long skuId,
                                          Model model){


        //返回详情页 skuDefaultImg\skuName\  SeckillGoods
        SeckillGoods detail = seckillFeignClient.getSeckillGoodsDetail(skuId);
        model.addAttribute("item",detail);
        return "seckill/item";
    }


    //

    /**
     * /seckill/queue.html?skuId='+this.skuId+'&skuIdStr='+skuIdStr
     *
     * 页面会每隔3秒给服务器发送请求检查是否成功.
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String toQueuePage(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuIdStr") String skuIdStr,
                              Model model){

        //
        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",skuIdStr);
        return "seckill/queue";
    }

    /**
     * 秒杀订单信息确认页
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String tradePage(Model model){

        OrderInfo orderInfo = seckillFeignClient.getUserSeckillOrderInfo();
        List<UserAddress> currentUserAddress = userFeignClient.getCurrentUserAddress();
        //detailArrayList、totalNum、totalAmount、userAddressList

        model.addAttribute("detailArrayList",orderInfo.getOrderDetailList());
        model.addAttribute("totalNum",1);
        model.addAttribute("totalAmount",orderInfo.getTotalAmount());
        model.addAttribute("userAddressList",currentUserAddress);
        return "seckill/trade";
    }
}
